package com.fastloan.mgt.invest.controller.temp;

import com.fastloan.mgt.invest.model.InvestTempVar;
import com.fastloan.mgt.invest.service.InvestTempVarService;
import com.kawins.base.baseabs.BaseController;
import com.kawins.result.ResultEnum;
import com.kawins.result.ResultTo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 模版表单管理
 *
 * @author rankai
 *         createTime 2017-08-2017/8/8 10:40
 */
@RestController
@RequestMapping("/api/invest/tempVar")
public class InvestTempVarController extends BaseController<InvestTempVar> {

    @Autowired
    private InvestTempVarService investTempVarService;


    /**
     * 获取模版表单数据
     *
     * @param tempId 模版Id
     * @return
     */
    @GetMapping("/{tempId}")
    public ResultTo select(@PathVariable("tempId") String tempId
            , @RequestParam(value = "typeId", defaultValue = "0") String typeId) {
        return new ResultTo().setData(investTempVarService.selectByTempId(tempId, typeId));
    }

    /**
     * 增加模版表单数据
     *
     * @param tempId  模版ID
     * @param fromIds 题目ID数组
     * @return
     */
    @PostMapping
    public ResultTo insert(@RequestParam("tempId") String tempId,
                           @RequestParam("fromIds") String fromIds) {
        if (StringUtils.isBlank(fromIds)) {
            return new ResultTo(ResultEnum.PARAM_ERROR);
        }
        investTempVarService.insertSort(tempId, fromIds);
        return new ResultTo();
    }

    /**
     * 增加模版表单数据
     *
     * @param investTempVar 模版表单Bean
     * @return
     */
    @PostMapping("/one")
    public ResultTo insert(InvestTempVar investTempVar) {
        String investTempVarId = investTempVarService.insertSortOne(investTempVar);
        return new ResultTo().setData(investTempVarId);
    }


    /**
     * 更新模版表单数据
     *
     * @param investTempVar 模版表单Bean
     * @return
     */
    @PutMapping
    public ResultTo update(InvestTempVar investTempVar) {
        investTempVarService.updateSort(investTempVar);
        return new ResultTo();
    }

    /**
     * 删除模版表单数据
     *
     * @param investTempVarId 模版表单Id
     * @return
     */
    @DeleteMapping("/{investTempVarId}")
    public ResultTo delete(@PathVariable("investTempVarId") Integer investTempVarId) {
        investTempVarService.deleteSort(investTempVarId);
        return new ResultTo();
    }
}
